local skill = fk.CreateSkill{
  name = "iberia__duoshi"
}

Fk:loadTranslationTable{
  [skill.name] = "夺势",
  [":"..skill.name] = "你可以跳过一个主要阶段并移动场上的一张装备牌。若以此法跳过判定或弃牌阶段，须移动你场上的牌。",

  [skill.name.. "-choose"] = "夺势：可以跳过%arg并移动自己场上的一张装备牌，请选择移动的终点角色",
  [skill.name.. "-prompt"] = "夺势：可以跳过%arg并移动场上的一张装备牌",
}

local phase_strings = {
  [Player.Judge] = "判定阶段",
  [Player.Draw] = "摸牌阶段",
  [Player.Play] = "出牌阶段",
  [Player.Discard] = "弃牌阶段",
}

skill:addEffect(fk.EventPhaseChanging, {
  anim_type = "control",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skill.name) and player == target and
      data.phase < Player.Finish and data.phase > Player.Start and not data.skipped then
        local room = player.room
        local move_self = table.contains({Player.Judge, Player.Discard}, data.phase)
        if move_self then
          local equips = player:getCardIds("e")
          return #equips > 0 and table.find(room:getOtherPlayers(player), function (p)
            for _, card in ipairs(equips) do
              if player:canMoveCardInBoardTo(p, card) then
                return true
              end
            end
          end)
        else
          return room:canMoveCardInBoard("e")
        end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local move_self = table.contains({Player.Judge, Player.Discard}, data.phase)
    if move_self then
      local equips = player:getCardIds("e")
      local tos = table.filter(room:getOtherPlayers(player), function (p)
        for _, card in ipairs(equips) do
          if player:canMoveCardInBoardTo(p, card) then
            return true
          end
        end
      end)
      if #tos > 0 then
        tos = room:askToChoosePlayers(player, {
          targets = tos,
          min_num = 1,
          max_num = 1,
          skill_name = skill.name,
          cancelable = true,
          no_indicate = true,
          prompt = skill.name.. "-choose:::"..phase_strings[data.phase],
        })
        if #tos > 0 then
          table.insert(tos, player)
          table.insert(tos, true)
          event:setCostData(self, tos)
          return true
        end
      end
    else
      local tos = room:askToChooseToMoveCardInBoard(player, {
        skill_name = skill.name,
        cancelable = true,
        flag = "e",
        prompt = skill.name.."-prompt:::"..phase_strings[data.phase],
      })
      if #tos == 2 then
        table.insert(tos, false)
        event:setCostData(self, tos)
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    data.skipped = true
    local tos = event:getCostData(self)
    if tos then
      player.room:askToMoveCardInBoard(player, {
        target_one = tos[1],
        target_two = tos[2],
        skill_name = skill.name,
        move_from = tos[3] and tos[2] or nil,
      })
    end
  end
})

return skill
